
OptimizeTab(g, NavIndex) {
	WICB:=20,SpaceItem:=16,C:=3
	Static sWCBT,sXCBT,sYCBT
	
	g["BtnSys_SaveOptimizeConfigTab"].Visible:=True
	
	try {
		g["BtnSelectAll"].Visible:=True
		g["HRLine_1"].Visible:=True
	} Catch {
		g["BGPanel"].GetPos(&sXCBT, &sYCBT, &PanelW)
		BtnSelectAll:=g.Add("Link","vBtnSelectAll Hidden w160 h20 x" sXCBT+(PanelW-160)/2 " y" (sYCBT+=12) " Background" Themes.%ThemeSelected%.BackColorPanelRGB, 
									'<a id="1">' GetLangText("Text_SelectAll") '</a>   <a id="0">' GetLangText("Text_DeselectAll") '</a>')
		BtnSelectAll.SetFont("s11")
		LinkUseDefaultColor(BtnSelectAll)
		BtnSelectAll.OnEvent("Click",BtnSelectAll_Click)
		BtnSelectAll.Visible:=True
		
		g.AddText("vHRLine_1 x" (sXCBT+(PanelW-400)/2) " y" (sYCBT+=30) " w400 h1 Background" Themes.%ThemeSelected%.HrColor).Focus()

		sWCBT:=(PanelW-SpaceItem)/C-SpaceItem
		sXCBT+=SpaceItem
		sYCBT+=SpaceItem
	}
	
	CurrentTabCtrls:=Array()

	x:=sXCBT
	y:=sYCBT
	i:=0,CtrlCreated:=1
	ItemList:=Layout[NavIndex].Items
	
	Loop ItemList.Length {
		ItemId:=ItemList[A_Index]
		
		If CtrlCreated {
			i++
			If i=1 {
			} Else If Mod(i,C)=1
				y+=30,x-=((C-1)*(SpaceItem+sWCBT))
			Else
				x+=(SpaceItem+sWCBT)
		}
	
		try {
			g[ItemId].Value:=CheckStatusItem(ItemId, Data.%ItemId%)
			CtrlCreated:=1
		} catch as err {
			CtrlCreated:=CreateCB(g,ItemId, sWCBT)
		} Finally {
			If CtrlCreated {
				g[ItemId].Move(x, y)
				g[ItemId].Visible:=True
				CurrentTabCtrls.Push ItemId
			}
		}
	}
	
	CurrentTabCtrls.Push "BtnSelectAll"
	CurrentTabCtrls.Push "HRLine_1"
	CurrentTabCtrls.Push "BtnSys_SaveOptimizeConfigTab"
	
	Return CurrentTabCtrls
}

CreateCB(g,ItemId, W) {
	s:=CheckStatusItem(ItemId, Data.%ItemId%)
	If s<=-1
		Return 0
	
	Static m:=Map()
	If m.Count=0 {	
		m["SWidth"]:=20
		m["SHeight"]:=20
		pToken:=Gdip_Startup()
		hValue1Icon:=Gdip_CreateARGBHBITMAPFromBase64('iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAJmWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgOS4xLWMwMDEgNzkuMTQ2Mjg5OSwgMjAyMy8wNi8yNS0yMDowMTo1NSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdEV2dD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlRXZlbnQjIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6cGhvdG9zaG9wPSJodHRwOi8vbnMuYWRvYmUuY29tL3Bob3Rvc2hvcC8xLj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pZDpmMGVhN2VlZi1kZGZiLWM1NDctOWIyMi02MTkzN2M3OGU5ZjIiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz5yI4PvAAAAiUlEQVQ4y+3UPQ6DMAyGYfcMwND7LyAhgSr1BKyM/IkzMNGhVRbzWXIXb47CluHZ4jdZYmJmSomSB6k+SmjhB+xwQgOFDb70wA6jw65zvQ0GmOEB5CDnV/jaoNwyOGN/Msc5mIM5GBOUvzxFxOQvL/Cxwbe+cnNum03nOht8ajQ492HQ1VfdurEvDgp6X54Vy6cAAAAASUVORK5CYII=')
		Gdip_Shutdown(pToken)
		m["Value1Icon"]:="HBITMAP:*" hValue1Icon
		m["Value0Icon"]:="HBITMAP:*" hValue0Icon
	}
	ThisCheckBox := g.AddPicSwitch("Hidden x0 y0 0x80 w" W " v" ItemId,GetLangName(ItemId),,m)
	g[ItemId].OnEvent("Click",CB_Click)
	g[ItemId].SPic.OnEvent("Click",(*)=>CB_Click(g[ItemId],""))
	g[ItemId].Value:=s
	Return 1
}
BtnSelectAll_Click(Ctr, ID, HREF) {
	g:=Ctr.Gui
	g2:=CreateWaitDlg(g)
	CurrentTabCtrls:=CurrentTabCtrlArray()
	Loop CurrentTabCtrls.Length {
		If g[CurrentTabCtrls[A_Index]].Type="PicSwitch" && g[CurrentTabCtrls[A_Index]].Value!=ID {
			g[CurrentTabCtrls[A_Index]].Value:=ID
			ProgNowCtr(g[CurrentTabCtrls[A_Index]],Data.%CurrentTabCtrls[A_Index]%,1)
		}
	}
	DestroyDlg(g,g2)
	SendMessage 0x8, 0, 0,Ctr,"A"
}

CB_Click(Ctr,Info) {
	g:=Ctr.Gui
	g2:=CreateWaitDlg(g)
	ProgNowCtr(Ctr,Data.%Ctr.Name%)
	DestroyDlg(g,g2)
}